package com.java.yz.mysql_jdbc;

import java.io.BufferedReader;
import java.io.FileReader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.Properties;

/*
    通过这个工具类获取与数据库的连接
 */
public class MySQLTool {
    //构造方法私有化
    private MySQLTool(){}
    public static Connection getPreparedStatement(){
        Connection connection = null;
        try {
            Properties properties = new Properties();
            //加载配置文件
            properties.load(new BufferedReader(new FileReader("src/com/java/yz/mysql_jdbc/mysqlinfo.properties")));
            //根据key获取对于的value值
            String url1 = properties.getProperty("url");
            String user1 = properties.getProperty("user");
            String password1 = properties.getProperty("password");


            //加载驱动
            Class.forName("com.mysql.jdbc.Driver");

            //获取连接对象
             connection = DriverManager.getConnection(url1, user1, password1);
        }catch (Exception e){
            e.printStackTrace();
        }
        return connection;

    }
}
